package org.kimoji.personal.dao;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.ibatis.session.SqlSession;
import org.kimoji.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Repository;

@Repository
public class PersonalDAO {

	private static final String USER = "user:";
	
	@Autowired
	private SqlSession sqlSession;
	@Autowired
	private RedisTemplate<String, User> redisTemplate;
	
	public List<User> loadAllUser(){
		return sqlSession.selectList("PersonalMapper.loadAllUser");
	}
	
	public boolean cacheAllUser(List<User> users){
		Map<String,User> userMap = new HashMap<String, User>();
		for(User user : users){
			userMap.put(USER + user.getId(), user);
		}
		redisTemplate.opsForValue().multiSet(userMap);
//		 boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {  
//	            @SuppressWarnings("unchecked")
//				public Boolean doInRedis(RedisConnection connection)  
//	                    throws DataAccessException {  
//	                for (User user : users) {  
//	                    byte[] key  = redisTemplate.getStringSerializer().serialize(USER + user.getId());  
//	                    byte[] value = (( RedisSerializer<User>)redisTemplate.getValueSerializer()).serialize(user);
//	                    connection.setNX(key, value);
//	                }
//	                return true;
//	            }
//	        }, false, true);
//	        return result;
		return true;
	}
	
	public Map<Integer,User> getUsersByUserIds(Set<String> ids){
		Set<String> userIds = new HashSet<String>();
		for(String id : ids){
			userIds.add(USER + id);
		}
		List<User> users = redisTemplate.opsForValue().multiGet(userIds);
		Map<Integer,User> map = new HashMap<Integer, User>();
		if(null != users && users.size()>0){
			for(User user : users){
				if(null != user)
					map.put(user.getId(), user);
			}
		}
		return map;
	}
	
	public User getUserByUserId(final String id){
		User result = redisTemplate.execute(new RedisCallback<User>() {  
            public User doInRedis(RedisConnection connection)  
                    throws DataAccessException {  
                byte[] key = redisTemplate.getStringSerializer().serialize(USER + id);  
                byte[] value = connection.get(key);
                if (value == null) {  
                    return null;  
                }  
                User user = (( RedisSerializer<User>)redisTemplate.getValueSerializer()).deserialize(value);
                return user;  
            }  
        });  
        return result; 
	}
	
}
